﻿@namespace Masa.Docs.Shared.Components
@inject I18n I18n
@inject MasaBlazor MasaBlazor
@implements IAsyncDisposable

<MTabs Class="default-tabs"
       Color="primary"
       Routable
       @ref="_tabs">
    @foreach (var item in _internalMenu)
    {
        <MTab Ripple="false"
              Href="@item.Href"
              Target="@item.Target"
              MatchPattern="@item.MatchPattern">
            @if (item.Href == "/")
            {
                <MBadge Color="primary"
                        Content="@Project"
                        Class="text-capitalize"
                        Value="@(!string.IsNullOrWhiteSpace(Project))">
                    @I18n.T(item.Title)
                </MBadge>
            }
            else
            {
                <MBadge Color="@item.StateBackgroundColor"
                        Content="@I18n.T(item.State)"
                        Value="@(!string.IsNullOrWhiteSpace(item.State))">
                    @I18n.T(item.Title)
                </MBadge>
            }
        </MTab>
    }

    @if (_resourcesMenu.Any())
    {
        <MDivider Vertical Class="ml-8 mr-4 my-auto" Style="height:16px" Inset/>

        <AppMenu Items="_resourcesMenu" TItem="DefaultItem" MinWidth="200">
            <ActivatorContent>
                <MButton Height="48" Text Class="m-tab" @attributes="@context.Attrs">
                    @I18n.T("resources") <AppChevronDown></AppChevronDown>
                </MButton>
            </ActivatorContent>
        </AppMenu>
    }
</MTabs>

@code {

    [CascadingParameter(Name = "Culture")] public string? Culture { get; set; }

    [CascadingParameter(Name = "project")] public string? Project { get; set; }

    private MTabs? _tabs;
    private string? _prevProject;
    private List<DefaultItem> _internalMenu = new();
    private List<DefaultItem> _resourcesMenu = new();
    private string? _prevCulture;

    protected override void OnInitialized()
    {
        base.OnInitialized();

        _prevCulture = Culture;
        MasaBlazor.RTLChanged += MasaBlazorOnRTLChanged;
    }

    private void MasaBlazorOnRTLChanged(object? sender, EventArgs e)
    {
        InvokeAsync(() =>
        {
            _tabs?.CallSlider();
            StateHasChanged();
        });
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        if (_prevProject is null || _prevProject != Project)
        {
            _prevProject = Project;
            _internalMenu = AppService.GetNavMenus(Project);
            _resourcesMenu = AppService.GetResources(Project);
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        if (_prevCulture != Culture)
        {
            _prevCulture = Culture;
            _tabs?.CallSlider();
        }
    }

    public async ValueTask DisposeAsync()
    {
        if (_tabs != null)
        {
            await _tabs.DisposeAsync();
        }

        MasaBlazor.RTLChanged -= MasaBlazorOnRTLChanged;
    }

}